from django.http import JsonResponse
from django.shortcuts import render,redirect,HttpResponse
from django.views.decorators.csrf import csrf_exempt

from app02.utils.bootrstap import BootrsapModelForm
from app02 import models

#学生首页
from app02.utils.pegination import Pegination


def student_list(request):
    return render(request,'student_view.html')


#课表信息
def student_course(request):
    if request.method=='GET':
        title="课表信息"
        form=models.CourseTable.objects.all()
        return render(request,'student_course.html',{'form':form,'title':title})

#请假信息管理
#创建ModelForm
class StudentLeaveModelForm(BootrsapModelForm):
    class Meta:
        #关联表
        model=models.StudentLeave
        exclude=['admins','exam','teacher_exam']

#请假信息管理
def student_leave(request):
    if request.method == 'GET':
        title = '请假信息'
        form_data =StudentLeaveModelForm()
        # 搜索框
        seach_list = request.GET.get('q', '')
        data_t = {}
        # 搜索框中内容对应sections节次
        if seach_list:
            data_t['name__contains'] = seach_list

        # 获取用户提交的信息
        student_dest = models.StudentLeave.objects.filter(**data_t)

        # 分页
        page_object = Pegination(request, student_dest, page_size=6)
        context = {
            'student_dest': page_object.page_mun,
            'page_string': page_object.html(),
            'seach_list': seach_list,
            'title': title,
            'form_data': form_data
        }
        return render(request, "student_leave.html", context)

#新建请假信息
@csrf_exempt
def student_leaveAdd(request):
    form_data=StudentLeaveModelForm(data=request.POST)
    if form_data.is_valid():
        form_data.save()
        data_dict={'static':True}
        return  JsonResponse(data_dict)
    return JsonResponse({'static':False,'error':form_data.errors})

#销假窗口
def student_LeaveReporting(request):
    #获取id
    uid=request.GET.get('uid')
    # 根据id获取学生信息表对应的那一列的数据
    leave_Object=models.StudentLeave.objects.filter(id=uid).values('id','name','sex','age','xueID','test','times','retime','level','teachers').first()

    #判断数据存不存在
    if not leave_Object:
        return JsonResponse({'static':False,'error':'数据不存在'})
    result={'static':True,'data':leave_Object}
    return JsonResponse(result)

#销假
@csrf_exempt
def student_reporting(request):
    #根据id获取课程信息表对应的那一列的数据
    uid=request.GET.get('uid')
    # 根据id获取课程信息表对应的那一列的数据
    leave_test=models.StudentLeave.objects.filter(id=uid).first()
    if not leave_test:
        return JsonResponse({'static':False,'tpis':'数据不存在'})
    form_data=StudentLeaveModelForm(data=request.POST,instance=leave_test)
    if form_data.is_valid():
        form_data.save()
        return JsonResponse({'static':True})
    return HttpResponse(JsonResponse({'static':False,'error':form_data.errors}))

#删除请假
def student_delete(request):
      uid=request.GET.get('uid')
      exits=models.StudentLeave.objects.filter(id=uid).exists()
      if not exits:
          return JsonResponse({'static':False,'error':'删除失败，数据不存在'})
      models.StudentLeave.objects.filter(id=uid).delete()
      return JsonResponse({'static':True})


#学生成绩
def student_Score(request):
    if request.method == 'GET':
        title = '成绩信息表'


        # 获取用户提交的信息
        name = request.session['info'].get('user')
        students = models.StudentInfo.objects.get(user=name)
        student_dest=students.scores.all()
        for student in student_dest:
            total_score = student.language + student.maths + student.English + student.history + student.Geography + student.political
            student.total =total_score

        # 分页
        page_object = Pegination(request,student_dest, page_size=6)
        context = {
            'student_dest': page_object.page_mun,
            'page_string': page_object.html(),
            'title': title,
        }
        return render(request, "student_score.html", context)

#教学评价信息
class RmationtableModelForm(BootrsapModelForm):
    class Meta:
        #关联表
        model=models.InformationTables
        fields='__all__'


#学生填写信息提交表单
class StudnetRmationtableModelForm(BootrsapModelForm):
    class Meta:
        #关联表
        model=models.StudentInformationTable
        exclude=['teacherRmation','tables','examRmation','adminRmation']
def student_Rmationlist(request):
    if request.method == 'GET':
        title = '教学评价信息收集表'
        form_data =StudnetRmationtableModelForm()
        # 搜索框
        seach_list = request.GET.get('q', '')
        data_t = {}
        # 搜索框中内容对应数据库中字段
        if seach_list:
            data_t['StudentName__contains'] = seach_list

        # 获取用户提交的信息
        student_dest=models.InformationTables.objects.filter(**data_t)


        # 分页
        page_object = Pegination(request, student_dest, page_size=6)
        context = {
            'student_dest':page_object.page_mun,
            'page_string': page_object.html(),
            'seach_list': seach_list,
            'title': title,
            'form_data': form_data
        }
        return render(request, "student_Information.html", context)
#提交信息
@csrf_exempt
def student_RmationAdd(request):
    form_data=StudnetRmationtableModelForm(data=request.POST)
    if form_data.is_valid():
        form_data.save()
        data_dict={'static':True}
        return  JsonResponse(data_dict)
    return JsonResponse({'static':False,'error':form_data.errors})


